package com.yzt.minemodule.wallet;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


import com.yzt.minemodule.R;
import com.yzt.zhuju.RXingUtils;
import com.yzt.zhuju.mvp.MVPActivity;
import com.yzt.zhuju.network.AresSubscriber;
import com.yzt.zhuju.network.biz.BizLayer;
import com.yzt.zhuju.network.entity.home.UserInfoBean;
import com.yzt.zhuju.rx.AsyncCall;
import com.yzt.zhuju.widget.tuiview.TUITextView;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

@SuppressLint({"NonConstantResourceId", "SetTextI18n"})
public class AddBankCardActivity extends MVPActivity {

    EditText tv_number;

    TextView et_phone;

    TextView tv_get_code;

    EditText tv_verify_code;

    TUITextView tv_next;

    private String number;

    @Override
    public int getLayoutId() {
        return R.layout.activity_add_bank_card;
    }

    @Override
    public void installViews() {
        mTitleBar.setTitle("修改银行卡");
    }

    public static void startSelf(Context activity, String bankNumber) {
        Intent intent = new Intent(activity, AddBankCardActivity.class);
        intent.putExtra("extra_id", bankNumber);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        number = getIntent().getStringExtra("extra_id");
        tv_number = findViewById(R.id.tv_number);
//        tv_number.setText(number);
        et_phone = findViewById(R.id.et_phone);
        tv_get_code = findViewById(R.id.tv_get_code);
        tv_get_code.setOnClickListener(this);
        tv_verify_code = findViewById(R.id.tv_verify_code);
        tv_next = findViewById(R.id.tv_next);
        tv_next.setOnClickListener(this);
        getAccountBindInfo();
    }

    /**
     * 获取账号的绑定状态
     */
    private void getAccountBindInfo() {
        BizLayer.getInstance().getAccountModule().getUserFullInfo(RXingUtils.getUserId(), new AresSubscriber<UserInfoBean>() {
            @Override
            protected void onSuccess(UserInfoBean personalDetail) {
                et_phone.setText(personalDetail.getPhone());
            }
        });
    }

    @Override
    protected void onClick(int clickId) {
        super.onClick(clickId);

        if (clickId == R.id.tv_get_code) {
            // 获取验证码
            getCode(et_phone.getText().toString());
        }

        if (clickId == R.id.tv_next) {
            String number = tv_number.getText().toString().trim();
            String msg = tv_verify_code.getText().toString().trim();

            if (number.equals("")) {
                showToast("请输入银行卡号");
                return;
            }

            if (msg.equals("")) {
                showToast("请输入验证码");
                return;
            }

            if (msg.length() != 6) {
                showToast("请输入正确的验证码");
                return;
            }

            putPageData();
        }
    }

    /**
     * 获取验证码
     */
    private void getCode(String phone) {
        BizLayer.getInstance().getAccountModule().getBankCardMsg(phone, new AresSubscriber<Object>() {
            @Override
            protected void onSuccess(Object personalDetail) {
                startCountCode();
            }
        });
    }

    private void putPageData() {
        String number = tv_number.getText().toString().trim();
        String msg = tv_verify_code.getText().toString().trim();
        BizLayer.getInstance().getAccountModule().bankCardUpdate(number, msg, new AresSubscriber<Object>() {
            @Override
            protected void onSuccess(Object personalDetail) {
                showToast("编辑成功！");
                finish();
            }
        });
    }

    @SuppressLint("CheckResult")
    public void startCountCode() {
        final int count = 60;
        Observable.interval(0, 1, TimeUnit.SECONDS).take(count + 1).compose(new AsyncCall<>()).compose(bindOnDestroy()).doOnSubscribe(new Consumer<Disposable>() {
            @Override
            public void accept(Disposable disposable) {
                tv_get_code.setEnabled(false);
            }
        }).subscribe(new Consumer<Long>() {
            @Override
            public void accept(Long aLong) {
                tv_get_code.setText((count - aLong) + "s");
                tv_get_code.setEnabled(false);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) {

            }
        }, new Action() {
            @Override
            public void run() {
                tv_get_code.setText("获取验证码");
                tv_get_code.setEnabled(true);
            }
        });
    }
}